package cn.b.sky.mes.mm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.model.RequirementTriggerMode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.condition.RequirementTriggerModeCondition;
import cn.b.sky.mes.mm.service.RequirementTriggerModeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

/**
 * 
 * <strong>Title : RequirementTriggerModeController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.mm.controller<br></strong>
 * <strong>Description : </strong>需求触发策略控制器<br> 
 * <strong>Create on : 2016-9-10 下午1:22:33<br></strong>
 * <p>
 * @author JinQuan.Fu<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/requirementTriggerMode/*")
public class RequirementTriggerModeController extends SecurityController {


  @Autowired
  RequirementTriggerModeService requirementTriggerModeService;

  @RequestMapping("requirementTriggerMode_list")
  public String requirementTriggerModeListByCondition(String navTabId, Model model,
      @ModelAttribute("condition")RequirementTriggerModeCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setOrderField("CreateDateTime");
    List<RequirementTriggerMode> list = requirementTriggerModeService.requirementTriggerModeListByCondition(condition);
    int count = requirementTriggerModeService.requirementTriggerModeCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return "mm/requirementTriggerMode/requirementTriggerMode_list";
  }

  @RequestMapping("viewRequirementTriggerMode")
  public String viewRequirementTriggerMode(String navTabId, Model model,
      String requirementTriggerModeCode) {
    RequirementTriggerMode requirementTriggerMode = requirementTriggerModeService.getRequirementTriggerMode(getSiteCode(), requirementTriggerModeCode);
    model.addAttribute("requirementTriggerMode", requirementTriggerMode);
    model.addAttribute(NAVTABID, navTabId);
    return "mm/requirementTriggerMode/requirementTriggerMode_view";
  }
  
  @RequestMapping("addRequirementTriggerMode")
  public String addRequirementTriggerMode(String navTabId, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    return "mm/requirementTriggerMode/requirementTriggerMode_add";
  }
  
  @RequestMapping("modifyRequirementTriggerMode")
  public String modifyRequirementTriggerMode(String navTabId, Model model,
      String requirementTriggerModeCode) {
    RequirementTriggerMode requirementTriggerMode = requirementTriggerModeService.getRequirementTriggerMode(getSiteCode(), requirementTriggerModeCode);
    model.addAttribute("requirementTriggerMode", requirementTriggerMode);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "modify");
    return "mm/requirementTriggerMode/requirementTriggerMode_add";
  }
  
  @RequestMapping("validateRequirementTriggerMode")
  @ResponseBody
  public boolean validateRequirementTriggerMode(String requirementTriggerModeCode) {
    if (StringUtils.isBlank(requirementTriggerModeCode)) {
      return true;
    } else {
      RequirementTriggerMode requirementTriggerMode = requirementTriggerModeService.getRequirementTriggerMode(getSiteCode(),requirementTriggerModeCode.toUpperCase());
      return requirementTriggerMode == null;
    }
  }
  
  @RequestMapping("saveRequirementTriggerMode")
  @ResponseBody
  public DWZResult saveRequirementTriggerMode(String id,RequirementTriggerMode requirementTriggerMode, String navTabId, Model model) {
    if (null == requirementTriggerMode) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    requirementTriggerMode.setSiteCode(this.getSiteCode());
    if (StringUtils.isBlank(id)) {
      setCreateInfo(requirementTriggerMode);
      requirementTriggerModeService.insertRequirementTriggerMode(requirementTriggerMode);
    } else {
      setModifyInfo(requirementTriggerMode);
      requirementTriggerModeService.updateRequirementTriggerMode(requirementTriggerMode);
    }
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
  }
  
  @RequestMapping("deleteRequirementTriggerMode")
  @ResponseBody
  public DWZResult deleteRequirementTriggerMode(String navTabId, Model model, String codes, HttpServletRequest request) {
    String[] idArr = codes.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      String code = idArr[i];
      String message = null;
      message = requirementTriggerModeService.deteleRequirementTriggerMode(getSiteCode(), code);
      boolean flag = true;
      if (!StringUtils.isBlank(message)) {
        failures += 1;
        flag = false;
        message = getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), message);
      }
      BatchJobReport report = new BatchJobReport(idArr[i], message, flag);
      reports.add(report);
    }
    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(this.getCurrentLanguage(), "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }
  
  
}
